/**
 * 
 */
package mdb.com.vn.web.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import mdb.com.vn.model.Casa;
import mdb.com.vn.model.FixedDepositCasa;
import mdb.com.vn.model.FixedDeposits;
import mdb.com.vn.model.ScratchCard;
import mdb.com.vn.services.MDBServices;
import mdb.com.vn.utils.NumberUtils;
import mdb.com.vn.utils.TextUtil;

/**
 * @author tu.dod
 * 
 */
@ManagedBean(name = "scratchCardBeans")
@ViewScoped
public class ScratchCardBeans implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String savingBookNo;
	private int number20k;
	private int number50k;
	private int number100k;
	private int number200k;
	private int number500k;

	private int number20kNotInput;
	private int number50kNotInput;
	private int number100kNotInput;
	private int number200kNotInput;
	private int number500kNotInput;

	private String detail20k;
	private String detail50k;
	private String detail100k;
	private String detail200k;
	private String detail500k;
	private boolean btnVisible;
	private boolean btnSaveVisible;
	private int totalScradAvailable;

	private FixedDeposits fixedDeposits;
	private Casa casa;
	private FixedDepositCasa fixedDepositCasa;
	private List<ScratchCard> scratchCards;

	@ManagedProperty("#{mdbServices}")
	private MDBServices mdbServices;

	@PostConstruct
	public void init() {
		savingBookNo = "";
		scratchCards = new ArrayList<ScratchCard>();
	}

	public void search2updateScratchCards() {
		scratchCards = new ArrayList<ScratchCard>();
		scratchCards = mdbServices.getListOfScratchCardByTranRef(savingBookNo);
		if (scratchCards.isEmpty()) {
			scratchCards = mdbServices
					.getListOfScratchCardByTranRef1(savingBookNo);
		}
		if (!scratchCards.isEmpty()) {
			for (ScratchCard scratchCard : scratchCards) {
				double deno = scratchCard.getScDenomination();
				int countComa = TextUtil.countedString(
						scratchCard.getScDesription(), ",");
				if (countComa == 0 && scratchCard.getScDesription() == null) {
					countComa = 0;
				} else {
					countComa = countComa + 1;
				}
				int quality = scratchCard.getQuantity();
				if (countComa <= quality) {

					if (deno == NumberUtils.VND20000) {
						setNumber20k(quality);
						setNumber20kNotInput(quality - countComa);
						setDetail20k(scratchCard.getScDesription());
					}
					if (deno == NumberUtils.VND50000) {
						setNumber50k(quality);
						setNumber50kNotInput(quality - countComa);
						setDetail50k(scratchCard.getScDesription());
					}
					if (deno == NumberUtils.VND100000) {
						setNumber100k(quality);
						setNumber100kNotInput(quality - countComa);
						setDetail100k(scratchCard.getScDesription());
					}
					if (deno == NumberUtils.VND200000) {
						setNumber200k(quality);
						setNumber200kNotInput(quality - countComa);
						setDetail200k(scratchCard.getScDesription());
					}
					if (deno == NumberUtils.VND500000) {
						setNumber500k(quality);
						setNumber500kNotInput(quality - countComa);
						setDetail500k(scratchCard.getScDesription());
					}

				}

			}
		}else{
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Sổ tiết kiệm không tồn tại hoặc chưa đăng ký tham gia chương trình khuyến mãi",
							"Vui lòng kiểm tra lại"));
		}
	}

	public void updateScratchCardDetails() {

		try {
			for (ScratchCard scratchCard : scratchCards) {
				double deno = scratchCard.getScDenomination();
				if (deno == NumberUtils.VND20000) {
					int countInput = TextUtil.countedStringPlus(getDetail20k(), ",");
					if (countInput <= scratchCard
							.getQuantity()) {
						scratchCard.setScDesription(getDetail20k());
						mdbServices.updateScratchCard(scratchCard);
					} else {
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_WARN,
										"Số lượng thẻ cào 20,000 vượt quá số lượng thẻ cào cho phép",
										"Bạn đã nhập dư "+ (countInput - scratchCard.getQuantity())+" thẻ cào"));
					}

				}
				if (deno == NumberUtils.VND50000) {
					int countInput = TextUtil.countedStringPlus(getDetail50k(), ",");
					if (countInput <= scratchCard
							.getQuantity()) {
						scratchCard.setScDesription(getDetail50k());
						mdbServices.updateScratchCard(scratchCard);
					} else {
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_WARN,
										"Số lượng thẻ cào 50,000 vượt quá số lượng thẻ cào cho phép",
										"Bạn đã nhập dư "+ (countInput - scratchCard.getQuantity())+" thẻ cào"));
					}
				}
				if (deno == NumberUtils.VND100000) {
					int countInput = TextUtil.countedStringPlus(getDetail100k(), ",");
					if (countInput <= scratchCard
							.getQuantity()) {
						scratchCard.setScDesription(getDetail100k());
						mdbServices.updateScratchCard(scratchCard);
					} else {
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_WARN,
										"Số lượng thẻ cào 100,000 vượt quá số lượng thẻ cào cho phép",
										"Bạn đã nhập dư "+ (countInput - scratchCard.getQuantity())+" thẻ cào"));
					}
				}
				if (deno == NumberUtils.VND200000) {
					int countInput = TextUtil.countedStringPlus(getDetail200k(), ",");
					if (countInput <= scratchCard
							.getQuantity()) {
						scratchCard.setScDesription(getDetail200k());
						mdbServices.updateScratchCard(scratchCard);
					} else {
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_WARN,
										"Số lượng thẻ cào 200,000 vượt quá số lượng thẻ cào cho phép",
										"Bạn đã nhập dư "+ (countInput - scratchCard.getQuantity())+" thẻ cào"));
					}
				}
				if (deno == NumberUtils.VND500000) {
					int countInput = TextUtil.countedStringPlus(getDetail500k(), ",");
					if (countInput <= scratchCard
							.getQuantity()) {
						scratchCard.setScDesription(getDetail500k());
						mdbServices.updateScratchCard(scratchCard);
					} else {
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_WARN,
										"Số lượng thẻ cào 500,000 vượt quá số lượng thẻ cào cho phép",
										"Bạn đã nhập dư "+ (countInput - scratchCard.getQuantity())+" thẻ cào"));
					}
				}

			}

			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							"Đã cập nhật hệ thống", ""));

		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Lỗi cập nhật:", e.getMessage()));

		}

	}

	public String getSavingBookNo() {
		return savingBookNo;
	}

	public void setSavingBookNo(String savingBookNo) {
		this.savingBookNo = savingBookNo;
	}

	public int getNumber20k() {
		return number20k;
	}

	public void setNumber20k(int number20k) {
		this.number20k = number20k;
	}

	public int getNumber50k() {
		return number50k;
	}

	public void setNumber50k(int number50k) {
		this.number50k = number50k;
	}

	public int getNumber100k() {
		return number100k;
	}

	public void setNumber100k(int number100k) {
		this.number100k = number100k;
	}

	public int getNumber200k() {
		return number200k;
	}

	public void setNumber200k(int number200k) {
		this.number200k = number200k;
	}

	public int getNumber500k() {
		return number500k;
	}

	public void setNumber500k(int number500k) {
		this.number500k = number500k;
	}

	public String getDetail20k() {
		return detail20k;
	}

	public void setDetail20k(String detail20k) {
		this.detail20k = detail20k;
	}

	public String getDetail50k() {
		return detail50k;
	}

	public void setDetail50k(String detail50k) {
		this.detail50k = detail50k;
	}

	public String getDetail100k() {
		return detail100k;
	}

	public void setDetail100k(String detail100k) {
		this.detail100k = detail100k;
	}

	public String getDetail200k() {
		return detail200k;
	}

	public void setDetail200k(String detail200k) {
		this.detail200k = detail200k;
	}

	public String getDetail500k() {
		return detail500k;
	}

	public void setDetail500k(String detail500k) {
		this.detail500k = detail500k;
	}

	public boolean isBtnVisible() {
		return btnVisible;
	}

	public void setBtnVisible(boolean btnVisible) {
		this.btnVisible = btnVisible;
	}

	public boolean isBtnSaveVisible() {
		return btnSaveVisible;
	}

	public void setBtnSaveVisible(boolean btnSaveVisible) {
		this.btnSaveVisible = btnSaveVisible;
	}

	public int getTotalScradAvailable() {
		return totalScradAvailable;
	}

	public void setTotalScradAvailable(int totalScradAvailable) {
		this.totalScradAvailable = totalScradAvailable;
	}

	public FixedDeposits getFixedDeposits() {
		return fixedDeposits;
	}

	public void setFixedDeposits(FixedDeposits fixedDeposits) {
		this.fixedDeposits = fixedDeposits;
	}

	public Casa getCasa() {
		return casa;
	}

	public void setCasa(Casa casa) {
		this.casa = casa;
	}

	public FixedDepositCasa getFixedDepositCasa() {
		return fixedDepositCasa;
	}

	public void setFixedDepositCasa(FixedDepositCasa fixedDepositCasa) {
		this.fixedDepositCasa = fixedDepositCasa;
	}

	public void setMdbServices(MDBServices mdbServices) {
		this.mdbServices = mdbServices;
	}

	public List<ScratchCard> getScratchCards() {
		return scratchCards;
	}

	public void setScratchCards(List<ScratchCard> scratchCards) {
		this.scratchCards = scratchCards;
	}

	public int getNumber20kNotInput() {
		return number20kNotInput;
	}

	public void setNumber20kNotInput(int number20kNotInput) {
		this.number20kNotInput = number20kNotInput;
	}

	public int getNumber50kNotInput() {
		return number50kNotInput;
	}

	public void setNumber50kNotInput(int number50kNotInput) {
		this.number50kNotInput = number50kNotInput;
	}

	public int getNumber100kNotInput() {
		return number100kNotInput;
	}

	public void setNumber100kNotInput(int number100kNotInput) {
		this.number100kNotInput = number100kNotInput;
	}

	public int getNumber200kNotInput() {
		return number200kNotInput;
	}

	public void setNumber200kNotInput(int number200kNotInput) {
		this.number200kNotInput = number200kNotInput;
	}

	public int getNumber500kNotInput() {
		return number500kNotInput;
	}

	public void setNumber500kNotInput(int number500kNotInput) {
		this.number500kNotInput = number500kNotInput;
	}

}
